package leetcode.code0401;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;

public class Solution implements IDebug {

	static List<String>[] hours, minutes;

	static int maxh = 3, maxm = 5, max = 8;
	static {
		hours = new ArrayList[maxh + 1];
		minutes = new ArrayList[maxm + 1];
		for (int i = 0; i <= maxh; i++) {
			hours[i] = new ArrayList<>();
		}
		for (int i = 0; i <= maxm; i++) {
			minutes[i] = new ArrayList<>();
		}

		for (int i = 0; i < 12; i++) {
			int cnt = count(i);
			hours[cnt].add(String.valueOf(i));
		}
		for (int i = 0; i < 60; i++) {
			int cnt = count(i);
			if (i < 10) {
				minutes[cnt].add("0" + i);
			} else {
				minutes[cnt].add(String.valueOf(i));
			}

		}
	}

	private static int count(int i) {
		int cnt = 0;
		while (i > 0) {
			cnt += (i & 1) == 1 ? 1 : 0;
			i >>= 1;
		}
		return cnt;
	}

	public List<String> readBinaryWatch(int turnedOn) {
		List<String> ans = new ArrayList<>();
		if (turnedOn > max) {
			return ans;
		}
		for (int hour = 0; hour <= Math.min(turnedOn, maxh); hour++) {
			int minute = turnedOn - hour;
			if (minute <= 5) {
				List<String> hs = hours[hour];
				List<String> ms = minutes[minute];
				for (String h : hs) {
					for (String m : ms) {
						ans.add(h + ":" + m);
					}
				}
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.readBinaryWatch(5);

	}

}
